using System;
using System.Drawing;
using System.Windows.Forms;

namespace WindowsFormsDataBaice
{
    /// <summary>
    /// 系统设置窗体
    /// 提供系统配置、用户管理、权限设置等功能
    /// </summary>
    public class SystemSettingsForm : Form
    {
        // 控件声明
        private TabControl tabControl;
        private TabPage tabGeneral;
        private TabPage tabUsers;
        private TabPage tabPermissions;
        private TabPage tabDepartments;
        
        // 通用设置控件
        private Label lblSystemName;
        private TextBox txtSystemName;
        private Label lblSystemVersion;
        private TextBox txtSystemVersion;
        private CheckBox chkAutoBackup;
        private CheckBox chkAutoUpdate;
        private Button btnSaveGeneral;
        
        // 用户管理控件
        private DataGridView dgvUsers;
        private Button btnAddUser;
        private Button btnEditUser;
        private Button btnDeleteUser;
        
        // 权限设置控件
        private TreeView treePermissions;
        private CheckedListBox lstRoles;
        private Button btnSavePermissions;
        
        // 科室管理控件
        private Button btnDepartmentManagement;
        private Label lblDepartmentNote;

        public SystemSettingsForm()
        {
            InitializeComponent();
            SetupEventHandlers();
            LoadData();
        }

        private void InitializeComponent()
        {
            this.Text = "系统设置";
            this.Size = new Size(900, 600);
            this.StartPosition = FormStartPosition.CenterScreen;
            this.BackColor = UIStyleManager.BackgroundColor;

            // 创建选项卡控件
            tabControl = new TabControl
            {
                Dock = DockStyle.Fill,
                Font = UIStyleManager.BodyFont
            };

            // 通用设置选项卡
            tabGeneral = new TabPage
            {
                Text = "通用设置",
                BackColor = Color.White
            };

            // 系统名称设置
            lblSystemName = new Label
            {
                Text = "系统名称:",
                Location = new Point(20, 20),
                AutoSize = true,
                Font = UIStyleManager.BodyFont
            };

            txtSystemName = new TextBox
            {
                Location = new Point(120, 20),
                Size = new Size(200, 25),
                Text = "医疗信息管理系统",
                Font = UIStyleManager.BodyFont
            };

            // 系统版本设置
            lblSystemVersion = new Label
            {
                Text = "系统版本:",
                Location = new Point(20, 60),
                AutoSize = true,
                Font = UIStyleManager.BodyFont
            };

            txtSystemVersion = new TextBox
            {
                Location = new Point(120, 60),
                Size = new Size(200, 25),
                Text = "v1.0.0",
                Font = UIStyleManager.BodyFont
            };

            // 自动备份设置
            chkAutoBackup = new CheckBox
            {
                Text = "启用自动备份",
                Location = new Point(20, 100),
                AutoSize = true,
                Checked = true,
                Font = UIStyleManager.BodyFont
            };

            // 自动更新设置
            chkAutoUpdate = new CheckBox
            {
                Text = "启用自动更新",
                Location = new Point(20, 130),
                AutoSize = true,
                Checked = false,
                Font = UIStyleManager.BodyFont
            };

            // 保存按钮
            btnSaveGeneral = new Button
            {
                Text = "保存设置",
                Location = new Point(120, 170),
                Size = new Size(100, 30),
                BackColor = UIStyleManager.PrimaryColor,
                ForeColor = Color.White,
                FlatStyle = FlatStyle.Flat,
                Font = UIStyleManager.BodyFont
            };

            // 添加控件到通用设置选项卡
            tabGeneral.Controls.AddRange(new Control[] {
                lblSystemName, txtSystemName,
                lblSystemVersion, txtSystemVersion,
                chkAutoBackup, chkAutoUpdate, btnSaveGeneral
            });

            // 用户管理选项卡
            tabUsers = new TabPage
            {
                Text = "用户管理",
                BackColor = Color.White
            };

            // 用户列表
            dgvUsers = new DataGridView
            {
                Location = new Point(20, 20),
                Size = new Size(600, 300),
                AllowUserToAddRows = false,
                ReadOnly = true,
                AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.Fill,
                BackgroundColor = Color.White
            };

            // 设置用户列表列
            dgvUsers.Columns.Add("Username", "用户名");
            dgvUsers.Columns.Add("RealName", "真实姓名");
            dgvUsers.Columns.Add("Role", "角色");
            dgvUsers.Columns.Add("Status", "状态");

            // 用户管理按钮
            btnAddUser = new Button
            {
                Text = "添加用户",
                Location = new Point(20, 340),
                Size = new Size(80, 30),
                BackColor = Color.FromArgb(52, 152, 219),
                ForeColor = Color.White,
                FlatStyle = FlatStyle.Flat
            };

            btnEditUser = new Button
            {
                Text = "编辑用户",
                Location = new Point(120, 340),
                Size = new Size(80, 30),
                BackColor = Color.FromArgb(46, 204, 113),
                ForeColor = Color.White,
                FlatStyle = FlatStyle.Flat
            };

            btnDeleteUser = new Button
            {
                Text = "删除用户",
                Location = new Point(220, 340),
                Size = new Size(80, 30),
                BackColor = Color.FromArgb(231, 76, 60),
                ForeColor = Color.White,
                FlatStyle = FlatStyle.Flat
            };

            // 添加控件到用户管理选项卡
            tabUsers.Controls.AddRange(new Control[] {
                dgvUsers, btnAddUser, btnEditUser, btnDeleteUser
            });

            // 权限设置选项卡
            tabPermissions = new TabPage
            {
                Text = "权限设置",
                BackColor = Color.White
            };

            // 权限树
            treePermissions = new TreeView
            {
                Location = new Point(20, 20),
                Size = new Size(300, 300),
                Font = UIStyleManager.BodyFont
            };

            // 角色列表
            lstRoles = new CheckedListBox
            {
                Location = new Point(340, 20),
                Size = new Size(200, 300),
                Font = UIStyleManager.BodyFont
            };

            // 保存权限按钮
            btnSavePermissions = new Button
            {
                Text = "保存权限",
                Location = new Point(20, 340),
                Size = new Size(100, 30),
                BackColor = UIStyleManager.PrimaryColor,
                ForeColor = Color.White,
                FlatStyle = FlatStyle.Flat
            };

            // 添加控件到权限设置选项卡
            tabPermissions.Controls.AddRange(new Control[] {
                treePermissions, lstRoles, btnSavePermissions
            });

            // 科室管理选项卡
            tabDepartments = new TabPage
            {
                Text = "科室管理",
                BackColor = Color.White
            };

            // 科室管理说明
            lblDepartmentNote = new Label
            {
                Text = "点击下方按钮进入科室管理界面，可以进行科室的增删改查操作。",
                Location = new Point(20, 20),
                Size = new Size(400, 40),
                Font = UIStyleManager.BodyFont
            };

            // 科室管理按钮
            btnDepartmentManagement = new Button
            {
                Text = "进入科室管理",
                Location = new Point(20, 80),
                Size = new Size(150, 40),
                BackColor = UIStyleManager.PrimaryColor,
                ForeColor = Color.White,
                FlatStyle = FlatStyle.Flat,
                Font = UIStyleManager.BodyFont
            };

            // 添加控件到科室管理选项卡
            tabDepartments.Controls.AddRange(new Control[] {
                lblDepartmentNote, btnDepartmentManagement
            });

            // 添加选项卡到选项卡控件
            tabControl.TabPages.AddRange(new TabPage[] {
                tabGeneral, tabUsers, tabPermissions, tabDepartments
            });

            // 添加选项卡控件到窗体
            this.Controls.Add(tabControl);
        }

        private void SetupEventHandlers()
        {
            btnSaveGeneral.Click += BtnSaveGeneral_Click;
            btnAddUser.Click += BtnAddUser_Click;
            btnEditUser.Click += BtnEditUser_Click;
            btnDeleteUser.Click += BtnDeleteUser_Click;
            btnSavePermissions.Click += BtnSavePermissions_Click;
            btnDepartmentManagement.Click += BtnDepartmentManagement_Click;
        }

        private void LoadData()
        {
            // 加载用户数据
            LoadUsers();
            
            // 加载权限数据
            LoadPermissions();
        }

        private void LoadUsers()
        {
            // 清空用户列表
            dgvUsers.Rows.Clear();

            // 添加示例用户数据
            dgvUsers.Rows.Add("admin", "系统管理员", "超级管理员", "启用");
            dgvUsers.Rows.Add("doctor1", "张医生", "医生", "启用");
            dgvUsers.Rows.Add("nurse1", "李护士", "护士", "启用");
            dgvUsers.Rows.Add("cashier1", "王收费员", "收费员", "启用");
        }

        private void LoadPermissions()
        {
            // 清空权限树
            treePermissions.Nodes.Clear();

            // 添加权限节点
            TreeNode systemNode = new TreeNode("系统管理");
            systemNode.Nodes.Add("用户管理");
            systemNode.Nodes.Add("角色管理");
            systemNode.Nodes.Add("权限设置");

            TreeNode outpatientNode = new TreeNode("门诊管理");
            outpatientNode.Nodes.Add("挂号管理");
            outpatientNode.Nodes.Add("医生管理");
            outpatientNode.Nodes.Add("收费管理");

            TreeNode inpatientNode = new TreeNode("住院管理");
            inpatientNode.Nodes.Add("入院管理");
            inpatientNode.Nodes.Add("病房管理");
            inpatientNode.Nodes.Add("出院管理");

            TreeNode pharmacyNode = new TreeNode("药房管理");
            pharmacyNode.Nodes.Add("药品管理");
            pharmacyNode.Nodes.Add("库存管理");
            pharmacyNode.Nodes.Add("处方管理");

            treePermissions.Nodes.AddRange(new TreeNode[] {
                systemNode, outpatientNode, inpatientNode, pharmacyNode
            });

            // 展开所有节点
            treePermissions.ExpandAll();

            // 清空角色列表
            lstRoles.Items.Clear();

            // 添加角色
            lstRoles.Items.Add("超级管理员");
            lstRoles.Items.Add("系统管理员");
            lstRoles.Items.Add("医生");
            lstRoles.Items.Add("护士");
            lstRoles.Items.Add("管理员");
            lstRoles.Items.Add("药房管理员");
            lstRoles.Items.Add("收费员");
        }

        private void BtnSaveGeneral_Click(object sender, EventArgs e)
        {
            try
            {
                // 保存通用设置
                MessageBox.Show("通用设置保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Console.WriteLine("通用设置已保存");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"保存通用设置失败: {ex.Message}");
                MessageBox.Show($"保存失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void BtnAddUser_Click(object sender, EventArgs e)
        {
            MessageBox.Show("添加用户功能", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
        }

        private void BtnEditUser_Click(object sender, EventArgs e)
        {
            if (dgvUsers.SelectedRows.Count > 0)
            {
                var selectedUser = dgvUsers.SelectedRows[0].Cells["Username"].Value.ToString();
                MessageBox.Show($"编辑用户: {selectedUser}", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
            {
                MessageBox.Show("请先选择要编辑的用户", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void BtnDeleteUser_Click(object sender, EventArgs e)
        {
            if (dgvUsers.SelectedRows.Count > 0)
            {
                var selectedUser = dgvUsers.SelectedRows[0].Cells["Username"].Value.ToString();
                var result = MessageBox.Show($"确定要删除用户 {selectedUser} 吗？", "确认删除", 
                    MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                
                if (result == DialogResult.Yes)
                {
                    dgvUsers.Rows.Remove(dgvUsers.SelectedRows[0]);
                    MessageBox.Show("用户删除成功", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
            else
            {
                MessageBox.Show("请先选择要删除的用户", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
        }

        private void BtnSavePermissions_Click(object sender, EventArgs e)
        {
            try
            {
                // 保存权限设置
                MessageBox.Show("权限设置保存成功！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
                Console.WriteLine("权限设置已保存");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"保存权限设置失败: {ex.Message}");
                MessageBox.Show($"保存失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void BtnDepartmentManagement_Click(object sender, EventArgs e)
        {
            try
            {
                // 打开科室管理窗体
                var departmentForm = new DepartmentManagementForm();
                departmentForm.ShowDialog();
                Console.WriteLine("打开科室管理窗体");
            }
            catch (Exception ex)
            {
                Console.WriteLine($"打开科室管理窗体失败: {ex.Message}");
                MessageBox.Show($"打开科室管理窗体失败: {ex.Message}", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
} 